package ro.peoplecentric.ctc.common.dao;

import java.io.Serializable;
import java.util.List;

/**
 * Every Dao interface should extend this interface. Created to expose general operations with DataBase, like:<br/>
 * update, get .... data.
 * 
 * @author pguzun
 */
public interface BaseDao {

    /**
     * Gets entity from DB by id.
     *
     * @param id , entity id
     * @return entity if found, null if not found and null if given id is null.
     */
    <T> T getById(Class<T> clazz, Serializable id);

    /**
     * Get all T entities.
     *
     * @param <T> entity type
     * @param clazz entity class
     * @return list of all entities
     */
    <T> List<T> getAll(Class<T> clazz);

    /**
     * Save or updates an entity instance.
     *
     * @param entity, the entity to save
     */
    void saveOrUpdate(Object entity);

    /**
     * Delete the entity.
     *
     * @param entity, the entity to delete
     */
    void delete(Object entity);

    /**
     * Count entities from DB.
     *
     * @param <T> entity type
     * @param clazz entity class
     * @return the count
     */
    <T> Long countAll(Class<T> clazz);

    /**
     * Get all T entities paginated.
     *
     * @param <T> entity type
     * @param clazz entity class
     * @param firstResult first result index
     * @param maxResults results in this query
     * @return the list of entities
     */
    <T> List<T> getAll(Class<T> clazz, int firstResult, int maxResults);

    /**
     * Flush the hibernate session. SHOULD BE USED ONLY FROM TESTS.
     *
     */
    void flush();

    /**
     * Clear the hibernate session. SHOULD BE USED ONLY FROM TESTS.
     */
    void clear();
}
